package com.zh.note.huawei.twohundred.上班之路;

/**
 * @Author: zhanghuan
 * @date: 2023/8/14 11:12
 * @description: create it
 */
public class Main {
    private static final int[][] offsets = {{0, 1, 1}, {0, -1, 2}, {1, 0, 3}, {-1, 0, 4}};
    private static int maxTurns = 0;
    private static int maxClears = 0;
    private static int numRows = 0;
    private static int numCols = 0;
    private static String[][] grid = null;

    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // maxTurns = sc.nextInt();
        // maxClears = sc.nextInt();
        // numRows = sc.nextInt();
        // numCols = sc.nextInt();
        // grid = new String[numRows][numCols];
        // for (int i = 0; i < numRows; i++) {
        //     grid[i] = sc.next().split("");
        // }
        maxTurns = 2;
        maxClears = 0;
        numRows = 5;
        numCols = 5;
        grid = new String[][]{{".", ".", "S", ".", "."}, {"*", "*", "*", "*", "."}, {"T", ".", ".", ".", "."}, {"*", "*", "*", "*", "."},
                {".", ".", ".", ".", "."}};
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                boolean[][] visited = new boolean[numRows][numCols];
                if ("S".equals(grid[i][j])) {
                    if (dfs(visited, i, j, 0, 0, 0)) {
                        System.out.println("YES");
                        return;
                    } else {
                        System.out.println("NO");
                        return;
                    }
                }

            }
        }
        System.out.println("NO");


    }

    private static boolean dfs(boolean[][] visited, int x, int y, int turnsUsed, int clearsUsed, int lastDirection) {
        if ("T".equals(grid[x][y])) {
            return true;
        }
        visited[x][y] = true;
        for (int i = 0; i < offsets.length; i++) {
            int currentDirection = offsets[i][2];
            int newX = x + offsets[i][0];
            int newY = y + offsets[i][1];
            boolean turnFlag = false;
            boolean breakFlag = false;
            if (newX >= 0 && newX < numRows && newY >= 0 && newY < numCols && !visited[newX][newY]) {
                if (lastDirection != 0 && lastDirection != currentDirection) { // 判断要不要拐弯
                    if (turnsUsed + 1 > maxTurns) {
                        continue;
                    }
                    turnFlag = true;
                }
                if ("*".equals(grid[newX][newY])) { // 判断要不要清理路障
                    if (clearsUsed + 1 > maxClears) {
                        continue;
                    }
                    breakFlag = true;
                }
                if (dfs(visited, newX, newY, turnsUsed + (turnFlag ? 1 : 0), clearsUsed + (breakFlag ? 1 : 0), currentDirection)) {
                    return true;
                }
            }
        }
        return false;
    }
}
